package com.bhh.myservlet.requestandresponse;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Map;

/**
 * @author bhh
 * @description 实现Request
 * <p>
 * 完成静态资源解析
 * 完成动态资源解析
 * <p>
 * 请求信息案例 : GET /image/1.png HTTP/1.1
 * @date Created in 2021-04-14 21:00
 * @modified By
 */
@Slf4j
public class NIORequest implements ServletRequest {
    private static final int DEFAULT_BUFFER_SIZE = 1024;

    private SocketChannel channel;
    private String uri;
    private ByteBuffer buffer;

    public NIORequest(SocketChannel channel) {
        this.channel = channel;
        buffer = ByteBuffer.allocate(DEFAULT_BUFFER_SIZE);
    }

    public String getUri() {
        return uri;
    }

    /**
     * 进行资源解析, 获取String类型的 uri
     * @throws IOException
     */
    public void parse() throws IOException {
        // 从流中读取uri地址
        // 转换为写模式
        buffer.clear();
        int read = 0;
        read = channel.read(buffer);

        //转换为读模式
        buffer.flip();

        // 读取地址为string类型
        StringBuilder request = new StringBuilder();
        for (int i = 0; i < read; i++) {
            request.append((char) buffer.get(i));
        }
        log.info("Request类第46行 请求信息:" + request.toString());
        // 解析uri
        uri = parseUri(request.toString());
    }

    /**
     * 解析 request获取 有效的请求的资源地址
     * @param request
     * @return
     */
    private String parseUri(String request) {
        int index1, index2;
        if ((index1 = request.indexOf(" ")) != -1) {

            if ((index2 = request.indexOf(" ", index1 + 1)) > index1) {
                // 注意: substring 从index1开始截取到index2 - 1处, 也就是包头不包尾
                return request.substring(index1 + 1, index2);
            }
        }
        return "";
    }

    @Override
    public Object getAttribute(String name) {
        return null;
    }

    @Override
    public Enumeration getAttributeNames() {
        return null;
    }

    @Override
    public String getCharacterEncoding() {
        return null;
    }

    @Override
    public void setCharacterEncoding(String env) throws UnsupportedEncodingException {

    }

    @Override
    public int getContentLength() {
        return 0;
    }

    @Override
    public String getContentType() {
        return null;
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        return null;
    }

    @Override
    public String getParameter(String name) {
        return null;
    }

    @Override
    public Enumeration getParameterNames() {
        return null;
    }

    @Override
    public String[] getParameterValues(String name) {
        return new String[0];
    }

    @Override
    public Map getParameterMap() {
        return null;
    }

    @Override
    public String getProtocol() {
        return null;
    }

    @Override
    public String getScheme() {
        return null;
    }

    @Override
    public String getServerName() {
        return null;
    }

    @Override
    public int getServerPort() {
        return 0;
    }

    @Override
    public BufferedReader getReader() throws IOException {
        return null;
    }

    @Override
    public String getRemoteAddr() {
        return null;
    }

    @Override
    public String getRemoteHost() {
        return null;
    }

    @Override
    public void setAttribute(String name, Object o) {

    }

    @Override
    public void removeAttribute(String name) {

    }

    @Override
    public Locale getLocale() {
        return null;
    }

    @Override
    public Enumeration getLocales() {
        return null;
    }

    @Override
    public boolean isSecure() {
        return false;
    }

    @Override
    public RequestDispatcher getRequestDispatcher(String path) {
        return null;
    }

    @Override
    public String getRealPath(String path) {
        return null;
    }

    @Override
    public int getRemotePort() {
        return 0;
    }

    @Override
    public String getLocalName() {
        return null;
    }

    @Override
    public String getLocalAddr() {
        return null;
    }

    @Override
    public int getLocalPort() {
        return 0;
    }
}
